package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult page(Integer categoryId, String name, Integer page, Integer pageSize, Integer status) {
        SetmealPageQueryDTO setmealPageQueryDTO = new SetmealPageQueryDTO();
        setmealPageQueryDTO.setPage(page);
        setmealPageQueryDTO.setPageSize(pageSize);
        setmealPageQueryDTO.setName(name);
        setmealPageQueryDTO.setStatus(status);
        setmealPageQueryDTO.setCategoryId(categoryId);
        PageHelper.startPage(page, pageSize);
        Page<SetmealVO> list = setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(list.getTotal(), list.getResult());
    }

    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        log.info("主体新增内容：{}", setmeal);
        setmealMapper.save(setmeal);
        log.info("主体新增成功");
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        log.info("菜品关系新增内容：{}", setmealDishes);
        setmealDishMapper.save(setmealDishes);
        log.info("菜品关系新增成功");
    }

    @Override
    @Transactional
    public void delete(Long[] ids) {
        setmealMapper.delete(ids);
        log.info("主体删除成功");
        for (Long id : ids) {
            setmealDishMapper.delete(id);
        }
        log.info("菜品关系删除成功");
    }

    @Override
    public SetmealVO findById(Long id) {
        log.info("id:{}", id);
        SetmealVO setmealVO = setmealMapper.findById(id);
        log.info("setmealVO:{}", setmealVO);
        List<SetmealDish> setmealDishes = setmealDishMapper.findBySetmealId(id);
        log.info(" setmealDishes:{}", setmealDishes);
        setmealVO.setSetmealDishes(setmealDishes);
        log.info("setmealVO:{}", setmealVO);
        return setmealVO;
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        // 创建一个新的套餐对象
        Setmeal setmeal = new Setmeal();
        // 将DTO中的属性复制到套餐对象中
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 更新数据库中的套餐信息
        setmealMapper.update(setmeal);
        log.info("主体更新完毕");
        // 获取套餐中的菜品列表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        log.info("获取套餐中的菜品列表setmealDishes:{}", setmealDishes);
        log.info("开始执行菜品列表更新");
        // 根据套餐ID删除原有的套餐菜品关联信息
        setmealDishMapper.delete(setmeal.getId());
        // 保存更新后的套餐菜品关联信息
        setmealDishMapper.save(setmealDishes);
    }

    @Override
    public void state(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }
}
